package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataTripInfoChangeRuleListDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataTravelTipsDTO;
import com.ctshk.rpc.system.dto.mian.data.TripInfoChangeRuleDTO;
import com.ctshk.rpc.system.entity.MainDataTravelTips;
import com.ctshk.rpc.system.entity.MainDataTripInfoChangeRule;
import com.ctshk.rpc.system.mapper.MainDataTripInfoChangeRuleMapper;
import com.ctshk.rpc.system.req.MainDataTripInfoChangeRuleAddReq;
import com.ctshk.rpc.system.req.MainDataTripInfoChangeRuleIdReq;
import com.ctshk.rpc.system.req.MainDataTripInfoChangeRuleListReq;
import com.ctshk.rpc.system.req.MainDataTripInfoChangeRuleUpdateReq;
import com.ctshk.rpc.system.req.main.data.MainDataTravelTipsQueryReq;
import com.ctshk.rpc.system.req.main.data.TripInfoChangeRuleIdReq;
import com.ctshk.rpc.system.req.main.data.TripInfoChangeRuleQueryReq;
import com.ctshk.rpc.system.req.main.data.TripInfoChangeRuleReq;
import com.ctshk.rpc.system.service.IMainDataTripInfoChangeRuleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 更改旅客资料收费规则 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-26
 */
@DubboService
public class MainDataTripInfoChangeRuleServiceImpl extends ServiceImpl<MainDataTripInfoChangeRuleMapper, MainDataTripInfoChangeRule> implements IMainDataTripInfoChangeRuleService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MainDataTripInfoChangeRuleAddReq req, TokenUser tokenUser) {
        MainDataTripInfoChangeRule mainDataTripInfoChangeRule = EntityUtil.copy(req, MainDataTripInfoChangeRule.class);
        long id = IdWorker.getId();
        LocalDateTime now = LocalDateTime.now();
        mainDataTripInfoChangeRule.setId(id);
        mainDataTripInfoChangeRule.setGmtCreate(now);
        mainDataTripInfoChangeRule.setGmtModified(now);
        mainDataTripInfoChangeRule.setCreateId(tokenUser.getId());
        mainDataTripInfoChangeRule.setModifiedId(tokenUser.getId());
        mainDataTripInfoChangeRule.setIsDeleted(IsDeletedCode.NO.getCode());
        mainDataTripInfoChangeRule.setGmtCreate(LocalDateTime.now());
        mainDataTripInfoChangeRule.setGmtModified(LocalDateTime.now());
        baseMapper.insert(mainDataTripInfoChangeRule);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MainDataTripInfoChangeRuleUpdateReq req, TokenUser tokenUser) {
        MainDataTripInfoChangeRule mainDataTripInfoChangeRule = EntityUtil.copy(req, MainDataTripInfoChangeRule.class);
        LocalDateTime now = LocalDateTime.now();
        mainDataTripInfoChangeRule.setGmtModified(now);
        mainDataTripInfoChangeRule.setModifiedId(tokenUser.getId());
        mainDataTripInfoChangeRule.setGmtModified(LocalDateTime.now());
        baseMapper.updateById(mainDataTripInfoChangeRule);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(MainDataTripInfoChangeRuleIdReq req, TokenUser tokenUser) {
        MainDataTripInfoChangeRule mainDataTripInfoChangeRule = EntityUtil.copy(req, MainDataTripInfoChangeRule.class);
        LocalDateTime now = LocalDateTime.now();
        mainDataTripInfoChangeRule.setGmtModified(now);
        mainDataTripInfoChangeRule.setModifiedId(tokenUser.getId());
        mainDataTripInfoChangeRule.setIsDeleted(IsDeletedCode.YES.getCode());
        baseMapper.updateById(mainDataTripInfoChangeRule);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(MainDataTripInfoChangeRuleIdReq req, TokenUser tokenUser) {
        MainDataTripInfoChangeRule mainDataTripInfoChangeRule = baseMapper.selectById(req.getId());
        if (null == mainDataTripInfoChangeRule) {
            throw new BusinessException(SystemError.SYS_411);
        }
        MainDataTripInfoChangeRule tripInfoChangeRule = new MainDataTripInfoChangeRule();
        tripInfoChangeRule.setModifiedId(tokenUser.getId());
        tripInfoChangeRule.setGmtModified(LocalDateTime.now());
        if (mainDataTripInfoChangeRule.getStatus() == 0) {
            tripInfoChangeRule.setId(mainDataTripInfoChangeRule.getId());
            tripInfoChangeRule.setStatus(1);
        }
        if (mainDataTripInfoChangeRule.getStatus() == 1) {
            tripInfoChangeRule.setId(mainDataTripInfoChangeRule.getId());
            tripInfoChangeRule.setStatus(0);
        }
        baseMapper.updateById(tripInfoChangeRule);
    }

    @Override
    public PageResponse<MainDataTripInfoChangeRuleListDTO> queryList(MainDataTripInfoChangeRuleListReq req) {
        Page<MainDataTripInfoChangeRule> page = new Page<>(req.getPageNo(),req.getPageSize());
        QueryWrapper<MainDataTripInfoChangeRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getTripInfoChangeRuleName()),MainDataTripInfoChangeRule::getTripInfoChangeRuleName,req.getTripInfoChangeRuleName());
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataTripInfoChangeRule::getStatus,req.getStatus());
        queryWrapper.lambda().eq(MainDataTripInfoChangeRule::getIsDeleted,IsDeletedCode.NO.getCode()).orderByDesc(MainDataTripInfoChangeRule::getGmtModified);
        Page<MainDataTripInfoChangeRule> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataTripInfoChangeRule> records = iPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataTripInfoChangeRuleListDTO> tripInfoChangeRuleListDTOList = EntityUtil.copyList(records, MainDataTripInfoChangeRuleListDTO.class);
        return new PageResponse<>(tripInfoChangeRuleListDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    public MainDataTripInfoChangeRuleListDTO query(MainDataTripInfoChangeRuleIdReq req) {
        MainDataTripInfoChangeRule mainDataTripInfoChangeRule = baseMapper.selectById(req.getId());
        MainDataTripInfoChangeRuleListDTO changeRuleListDTO = EntityUtil.copy(mainDataTripInfoChangeRule, MainDataTripInfoChangeRuleListDTO.class);
        return changeRuleListDTO;
    }
}
